package com.dengs.controller;

import com.dengs.UserFeignClient;
import com.dengs.entity.User;
import feign.Client;
import feign.Contract;
import feign.Feign;
import feign.auth.BasicAuthRequestInterceptor;
import feign.codec.Decoder;
import feign.codec.Encoder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.netflix.feign.FeignClientsConfiguration;
import org.springframework.context.annotation.Import;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;

@Controller
@Import(FeignClientsConfiguration.class)
public class MovieController {
    private UserFeignClient userFeignClient;
    private UserFeignClient adminFeignClient;

    @Autowired
    public MovieController(Decoder decoder, Encoder encoder, Client client, Contract contract) {
        this.userFeignClient = Feign.builder().client(client).
                encoder(encoder).decoder(decoder).contract(contract)
                .requestInterceptor(new BasicAuthRequestInterceptor("user","password1"))
                .target(UserFeignClient.class,"http://microservice-provider-user-with-auth/");
        this.adminFeignClient = Feign.builder().client(client).
                encoder(encoder).decoder(decoder).contract(contract)
                .requestInterceptor(new BasicAuthRequestInterceptor("admin","password2"))
                .target(UserFeignClient.class,"http://microservice-provider-user-with-auth/");
    }

    @GetMapping("/user-user/{id}")
    @ResponseBody
    public User findByIdUser(@PathVariable long id){
        return this.userFeignClient.findById(id);
    }

    @GetMapping("/user-admin/{id}")
    @ResponseBody
    public User findByIdAdmin(@PathVariable long id){
        return this.adminFeignClient.findById(id);
    }
}
